/* 如需修改此文件，请做好更新日志！ */
/* 2013.05.9 9:00  scrollXTS、scrollYTS中加入 检索按钮不参与滚屏*/

/* 2012.03.08 14:15 initScroll中使用代码为wrapper和scroller添加样式，不需要再引入样式文件，以方便使用 */

/* 掌厅版修改内容 */
/* scrollYTS修改e.target.tagName */
/* $.fn.addEventListener修改,添加参数，用于支持点击效果 */
/* 添加domQuickClickTS、domQuickClickTE接口 */
/* $.fn.addEventListener修改，添加对quickClick事件的支持 */
/* 修改scrollYTM */
/* scrollYTM、scrollYTE中事件响应函数调用专递参数顺序的修改 */
/* scrollYTS中去除e.stopPropagation */
/* domQuickClickTS和domQuickClickTE中暂时去除e.preventDefault */
/* scrollYTS中添加browserclick检测 */
/* intiScroll添加scrollBar参数 */
/* intiScroll添加scrollExTopHandler scrollingExBottomHandler scrollingExTopHandler */

/* 2012.08.27 12:00 修改尺寸单位px->em */
/* 2012.08.01 12:00 修改滚动效果，使之更流畅 */
/* 2012.07.31 12:00 添加scrollToBottom接口 */
/* 2012.07.15 12:00 修改了initScroll接口 */
/* 2012.06.15 12:00 滚动事件处理函数较大改动 */
/* 2012.05.08 11:24 修改createEvent scrollYTE scrollXTE使得在click taphold事件响应函数中使用e.stopPropagation()可以阻止事件的传播 */
/* 2012.05.07 15:00 添加resetScrollBar接口 */
/* 2012.05.05 17:59 scrollYTS和scrollXTS中添加e.stopPropagation防止嵌套的滚动 */
/* 2012.05.02 12:00 添加scrollToTop接口 */
/* 2012.04.26 10:56 修改X轴滚动,使用initScroll初始化 */
/* 2012.04.25 09:31 修改Y轴滚动，使之更流畅，依赖jquery.easing.js */
/* 2012.04.24 09:31 修改Y轴滚动，添加滚动条，添加initScrol接口 */
/* 2012.04.16 15:34 修改scrollYTE接口，支持滚动分页 */
/* 2012.04.09 10:55 修改create$li接口，支持arrow-r,minus类型 */
/* 2012.02.04 14:15 修改create$li接口，添加isScroll参数 */

/* 
 * 构建一个$("<li>")对象，为其添加UI和事件
 * @param detail Stirng <li>内的HTML字符流
 * @param type Stirng <li>的类型，可以为"delete","checkbox","radio","none"
 * @param eventSupport Boolean 是否添加默认的事件
 * @return Object JQuery对象
 * @author max
 */
function create$li(detail,type,isScroll,eventSupport){
	var liInnerHtml=undefined;
	switch(type){
		case "checkbox":
			liInnerHtml="<table><tr><td width=\"95%\">"+detail+"</td><td><span class=\"ui-icon ui-icon-checkbox-off\" style=\"display:block;\"></span></td></tr></table>";
			break;
		case "radio":
			liInnerHtml="<table><tr><td width=\"95%\">"+detail+"</td><td><span class=\"ui-icon ui-icon-radio-off\" style=\"display:block;\"></span></td></tr></table>";
			break;
		case "checkbox-2":
			liInnerHtml="<table><tr><td width=\"95%\">"+detail+"</td><td><button data-icon=\"check\" data-iconpos=\"notext\"></button></td></tr></table>";
			break;
		case "delete":
		case "arrow-r":
		case "minus":
			liInnerHtml="<table><tr><td width=\"95%\">"+detail+"</td><td><button data-icon=\""+type+"\" data-iconpos=\"notext\"></button></td></tr></table>";
			break;
		case "none":
		case null:
		case undefined:
			liInnerHtml=detail;
			break;
	}
	var $li=$("<li style=\"padding:0.5em;\" class=\"ui-li ui-li-static ui-body-c\"></li>");
	$li[0].innerHTML=liInnerHtml;

	switch(type){
		case "delete":
			var $button=$li.find("button:last");
			if(eventSupport!==false){
				if(isScroll){
					$button.addEventListener("click",function(){
						$li.remove();
					});
				}else{
					$button.click(function(){
						$li.remove();
					});
				}
			}
			$button.button();
			$button.closest("div").css("margin","0");
			break;
		case "checkbox-2":
			var $button=$li.find("button:last");
			if(eventSupport!==false){
				if(isScroll){
					$button.closest("td").addEventListener("click",function(e){
						if($button.closest("div").hasClass("ui-disabled")){
							$button.closest("div").removeClass("ui-disabled");
							$li.addClass("ui-btn-active");
						}
						else{
							$button.closest("div").addClass("ui-disabled");
							$li.removeClass("ui-btn-active");
						}
						e.stopPropagation();
					});
				}else{
					$button.closest("td").click(function(e){
						if($button.closest("div").hasClass("ui-disabled")){
							$button.closest("div").removeClass("ui-disabled");
							$li.addClass("ui-btn-active");
						}
						else{
							$button.closest("div").addClass("ui-disabled");
							$li.removeClass("ui-btn-active");
						}
						e.stopPropagation();
					});
				}
			}
			$button.button();
			$button.closest("div").addClass("ui-disabled");
			$button.closest("div").css("margin","0");
			break;
		case "checkbox":
			var $span=$li.find("span:last");
			if(eventSupport!==false){
				if(isScroll){
					$span.closest("td").addEventListener("click",function(e){
						if($span.hasClass("ui-icon-checkbox-off")){
							$span.removeClass("ui-icon-checkbox-off").addClass("ui-icon-checkbox-on");
						}
						else{
							$span.removeClass("ui-icon-checkbox-on").addClass("ui-icon-checkbox-off");
						}
						e.stopPropagation();
					});
				}else{
					$span.closest("td").click(function(e){
						if($span.hasClass("ui-icon-checkbox-off")){
							$span.removeClass("ui-icon-checkbox-off").addClass("ui-icon-checkbox-on");
						}
						else{
							$span.removeClass("ui-icon-checkbox-on").addClass("ui-icon-checkbox-off");
						}
						e.stopPropagation();
					});
				}
			}
			break;
		case "radio":
			var $span=$li.find("span:last");
			if(eventSupport!==false){
				if(isScroll){
					$span.closest("td").addEventListener("click",function(e){
						if($span.hasClass("ui-icon-radio-off")){
							var $checkedSpan=$span.closest("ul").find(".ui-icon-radio-on");
							$checkedSpan.removeClass("ui-icon-radio-on").addClass("ui-icon-radio-off");
							$span.removeClass("ui-icon-radio-off").addClass("ui-icon-radio-on");
						}
						e.stopPropagation();
					});
				}else{
					$span.closest("td").click(function(e){
						if($span.hasClass("ui-icon-radio-off")){
							var $checkedSpan=$span.closest("ul").find(".ui-icon-radio-on");
							$checkedSpan.removeClass("ui-icon-radio-on").addClass("ui-icon-radio-off");
							$span.removeClass("ui-icon-radio-off").addClass("ui-icon-radio-on");
						}
						e.stopPropagation();
					});
				}
			}
			break;
		case "arrow-r":
		case "minus":
			var $button=$li.find("button:last");
			$button.button();
			$button.closest("div").css("margin","0");
			break;
		case "none":
		case null:
		case undefined:
			break;
	}
		
	return $li;
}

/* 
 * 更新通过create$li()构建的$("<li>")对象
 * @param $li 通过create$li()构建的$("<li>")对象
 * @param detail Stirng <li>内的HTML字符流
 * @param type Stirng <li>的类型，可以为"delete","check","none"
 * @author max
 */
function update$li($li,detail,type){
	switch(type){
		case "delete":
		case "checkbox":
		case "radio":
		case "checkbox-2":
			$li.find("td:first")[0].innerHTML=detail;
			break;
		case "none":
		case null:
		case undefined:
			$li[0].innerHTML=detail;
			break;
	}
}

/* 
 * 返回选中的列表项 type：checkbox,radio
 * @return Object
 * @author max
 */
$.fn.getSelectedItems=function(){
	var selectedCheckboxs=this.find(".ui-icon-checkbox-off").length || this.find(".ui-icon-checkbox-on").length;
	if(selectedCheckboxs){
		return this.find(".ui-icon-checkbox-on").closest("li");
	}
	var selectedRadios=this.find(".ui-icon-radio-off").length || this.find(".ui-icon-radio-on");
	if(selectedRadios){
		return this.find(".ui-icon-radio-on").closest("li");
	}
}

/* 
 * 全选列表项 type：checkbox
 * @return Object
 * @author max
 */
$.fn.selectAllItems=function(){
	var selectedCheckboxs=this.find(".ui-icon-checkbox-off").length || this.find(".ui-icon-checkbox-on").length;
	if(selectedCheckboxs){
		this.find(".ui-icon-checkbox-off").removeClass("ui-icon-checkbox-off").addClass("ui-icon-checkbox-on")
	}
	return this;
}

/* 
 * 取消全选列表项 type：checkbox
 * @return Object
 * @author max
 */
$.fn.cancelSelectAllItems=function(){
	var selectedCheckboxs=this.find(".ui-icon-checkbox-off").length || this.find(".ui-icon-checkbox-on").length;
	if(selectedCheckboxs){
		this.find(".ui-icon-checkbox-on").removeClass("ui-icon-checkbox-on").addClass("ui-icon-checkbox-off")
	}
	return this;
}

/* 
 * 将Form中的表单项根据其name和value属性封装成对象
 * @param $form 封装Form的JQuery对象
 * @return Object
 * 备注：Form中的表单项须设置name属性，否则不会被封装
 * @author max
 */
function formToObject($form){
	var obj=new Object();
	$form.find("input,textarea,button,select").each(function(){
		if(this.name)
			obj[this.name]=this.value;
	});
	return obj;
}

/* 
 * 根据Object的属性名和值设置Form中的表单项的value
 * @param obj Object
 * @param $form 封装了被设置的Form的JQuery对象
 * 备注：Form中的表单项须设置name属性，否则不会被设置
 * @author max
 */
function objectToForm(obj,$form){
	$form[0].reset();
	$form.find("input,textarea,button").each(function(){
		if(this.name)
			this.value=obj[this.name];
	});
	$form.find("select").each(function(){
		for(var i=0;i<this.options.length;i++){
			if(!this.name && this.options[i].value==obj[this.name]){
				this.options[i].selected=true;
			}
		}
	});
}

/* 
 * 将对象数组转成可拼接在URL后传递的字符串
 * @param array 对象数组[object,obeject...]
 * @param ignoreId Boolean 是否忽略对象中的"id"属性，默认为true
 * @return 字符串
 * @author max
 */
function objArrayToURLString(array,ignoreId){
	var tempArr="";
	for(var i=0;i<array.length;i++){
		var tempObj="";
		for(var name in array[i]){
			if(name=="id" && ignoreId!==false)
				continue;
			tempObj=tempObj+"objMap[\""+i+"\"]."+name+"="+array[i][name]+"&";
		}
		tempArr=tempArr+tempObj;
	}
	
	return tempArr;
}

/* 
 * 将对象转成可拼接在URL后传递的字符串
 * @param obj Object
 * @param objName String 对象名
 * @param ignoreId Boolean 是否忽略对象中的"id"属性，默认为false
 * @return 字符串
 * @author max
 */
function objectToURLString(obj,objName,ignoreId){
	var tempStr="";
	for(var name in obj){
		if(name=="id" && ignoreId===true)
			continue;
		tempStr=tempStr+objName+"."+name+"="+obj[name]+"&";
	}
	return tempStr;
}

/* 
 * 初始化分页信息
 * window.pageNum 当前页
 * window.pageCount 总页数
 * @author max
 */
function initPage(){
	window.pageNum=0;
	window.pageCount=0;
}

/* 
 * 请求首页
 * @param func 点击请求首页按钮的响应函数
 * @author max
 */
function firstPage(func){
	if(window.pageNum==1){
		//alert("已经是第一页了！");
		return;
	}
	func.call(this,1);
}

/* 
 * 请求下一页
 * @param func 点击请求下一页按钮的响应函数
 * @author max
 */
function nextPage(func){
	if(window.pageNum==window.pageCount){
		//alert("已经是最后一页了！");
		return;
	}
	func.call(this,window.pageNum+1);
}

/* 
 * 请求上一页
 * @param func 点击请求上一页按钮的响应函数
 * @author max
 */
function prevPage(func){
	if(window.pageNum==1){
		//alert("已经是第一页了！");
		return;
	}
	func.call(this,window.pageNum-1);
}

/* 
 * 请求末页
 * @param func 点击请求末页按钮的响应函数
 * @author max
 */
function lastPage(func){
	if(window.pageNum==pageCount){
		//alert("已经是最后一页了！");
		return;
	}
	func.call(this,window.pageCount);
}

/* 
 * 打开数据库
 * @author max
 */
function openDataBase(){
	return openDatabase("mydb","1.0","",2*1024*1024);
}

/* 
 * 打开数据库表，检测表是否存在，如果不存在则建表
 * @param db 数据库对象
 * @param tableName 表名
 * @param schemaObj 如果建表，按照schemaObj中的属性名建表
 * @param success 建表成功响应函数
 * @param error 建表失败响应函数
 * @author max
 */
function openTable(db,tableName,schemaObj,success,error){
	var temp="id unique";
	for(var name in schemaObj){
		temp=temp+","+name;
	}
	db.transaction(function(tx){
		tx.executeSql("create table if not exists "+tableName+"("+temp+")",[],null,function(tx,err){
			alert("创建表失败！");
			if(error)
				error.call();
		});
	},function(){},function(){
		if(success)
			success.call();
	});
}

/* 
 * 插入数据对象
 * @param db 数据库对象
 * @param tableName 表名
 * @param obj 被插入的对象
 * @param success 成功响应函数
 * @param error 失败响应函数
 * @author max
 */
function insertObject(db,tableName,obj,success,error){
	var temp="id";
	var tempSymbol="?";
	var tempValues=new Array();
	tempValues.push(getUniqueInteger());
	for(var name in obj){
		temp=temp+","+name;
		tempSymbol=tempSymbol+",?";
		tempValues.push(obj[name]);
	}
	db.transaction(function(tx){
		tx.executeSql("insert into "+tableName+"("+temp+") values("+tempSymbol+")",tempValues,null,function(tx,err){
			alert("保存数据失败！");
			if(error)
				error.call();
		});
	},function(){},function(){
		if(success)
			success.call();
	});
}

/* 
 * 查询数据对象
 * @param db 数据库对象
 * @param tableName 表名
 * @param success 成功响应函数
 * @param error 失败响应函数
 * @author max
 */
function findObjects(db,tableName,success,error){
	var objects=new Array();
	db.transaction(function(tx){
		tx.executeSql("select * from "+tableName,[],function(tx,results){
			for(var i=0;i<results.rows.length;i++){
				objects.push(results.rows.item(i));
			}
			if(success)
				success.call(window,objects);
		},function(tx,err){
			alert("查询数据失败！");
			if(error)
				error.call();
		});
	},function(){},function(){});
}

/* 
 * 删除数据对象
 * @param db 数据库对象
 * @param tableName 表名
 * @param obj 被删除的对象
 * @param success 成功响应函数
 * @param error 失败响应函数
 * @author max
 */
function deleteObject(db,tableName,obj,success,error){
	db.transaction(function(tx){
		tx.executeSql("delete from "+tableName+" where id="+obj.id,[],null,function(tx,err){
			alert("删除数据失败！");
			if(error)
				error.call();
		});
	},function(){},function(){
		if(success)
			success.call();		
	});
}

/* 
 * 更新数据对象
 * @param db 数据库对象
 * @param tableName 表名
 * @param obj 被更新的对象
 * @param success 成功响应函数
 * @param error 失败响应函数
 * @author max
 */
function updateObject(db,tableName,obj,success,error){
	var temp="";
	var tempValues=new Array();
	for(var name in obj){
		if(name==="id")
			continue;
		temp=temp+", "+name+"=?";
		tempValues.push(obj[name]);
	}
	temp=temp.substring(2);
	db.transaction(function(tx){
		tx.executeSql("update "+tableName+" set "+temp+" where id="+obj.id,tempValues,null,function(tx,err){
			alert("更新数据失败！");
			if(error)
				error.call();
		});
	},function(){},function(){
		if(success)
			success.call();
	});
}

/* 
 * 每次执行获得一个唯一的整数
 * @return Number
 * @author max
 */
function getUniqueInteger(){
	var dt=new Date();
	return dt.getTime()+Math.floor(Math.random()*10*10*10);
}

(function(){
	window.supportTouch="ontouchstart" in window;
	START_EV = window.supportTouch ? "touchstart" : "mousedown";
	MOVE_EV = window.supportTouch ? "touchmove" : "mousemove";
	END_EV = window.supportTouch ? "touchend" : "mouseup";
})();

/* 
 * 初始化滚动
 * @param wrapper .jszx-wrapper的id
 * @param dir "x" or "y"
 * @param bounce true or false
 * @param scrollExBottomHandler function
 * @return Number
 * @author max
 */
function initScroll(options){
	
	var $wrapper=$("#"+options["wrapper"]+"");
	var $scroller=$wrapper.find("div.jszx-scroller");
	
	$wrapper.css({"position":"relative","overflow":"hidden"});
	$scroller.css({"position":"relative"});
	
	if($scroller.data("scrollBar"))
		return;
	
	if(options["dir"]=="x"){
		$scroller[0].addEventListener(START_EV,scrollXTS);
		return;
	}
	
	$scroller[0].addEventListener(START_EV,scrollYTS);

	var scrollBar=$("<div style='background:rgb(183, 183, 183);;opacity:0.8;width:4px;border-radius:2px;position:absolute;top:0px;right:1px;'></div>")
	var scrollerHeight=$scroller.height();
	var wrapperHeight=$wrapper.height();
	var scrollBarHeight=scrollerHeight<=wrapperHeight?0:Math.floor(wrapperHeight*wrapperHeight/scrollerHeight);
	scrollBar.height(scrollBarHeight);
	scrollBar.appendTo($wrapper);
	$scroller.data("scrollBar",scrollBar);
	
	if(options["scrollBar"]==false)
	    scrollBar.hide();
	
	$scroller.data("scrollExBottomHandler",options["scrollExBottomHandler"]);
	$scroller.data("scrollExTopHandler",options["scrollExTopHandler"]);
	
	$scroller.data("scrollingExBottomHandler",options["scrollingExBottomHandler"]);
	$scroller.data("scrollingExTopHandler",options["scrollingExTopHandler"]);
	
	$scroller.data("bounce",options["bounce"]||false);
	
}

/* 
 * 滚动到顶部
 * @param wrapper .jszx-wrapper的id
 * @author max
 */
function scrollToTop(wrapper){
	var $wrapper=$("#"+wrapper+"");
	var $scroller=$wrapper.find("div.jszx-scroller");
	
	$scroller[0].style.WebkitTransition="";
	$scroller.data("scrollBar")[0].style.WebkitTransition="";
		
	$scroller[0].style.top="0px";
	$scroller.data("scrollBar")[0].style.top="0px";
}

/* 
 * 滚动到底部
 * @param wrapper .jszx-wrapper的id
 * @author max
 */
function scrollToBottom(wrapper){
	var $wrapper=$("#"+wrapper+"");
	var $scroller=$wrapper.find("div.jszx-scroller");
	
	if($wrapper[0].clientHeight-$scroller[0].clientHeight>0){
		return;
	}else{
		$scroller[0].style.WebkitTransition="";
		$scroller.data("scrollBar")[0].style.WebkitTransition="";
		$scroller[0].style.top=$wrapper[0].clientHeight-$scroller[0].clientHeight+"px";
		$scroller.data("scrollBar")[0].style.top=$wrapper[0].clientHeight-$scroller.data("scrollBar")[0].clientHeight+"px"
	}
}

/* 
 * 重设滚动条
 * @param wrapper .jszx-wrapper的id
 * @param isTop 是否需要滑动到顶部
 * @author max
 */
function resetScrollBar(wrapper,isTop){
	var $wrapper=$("#"+wrapper);
	var $scroller=$wrapper.find("div.jszx-scroller");
	var $scrollerBar=$scroller.data("scrollBar");
	
	var scrollerHeight=$scroller.height();
	var wrapperHeight=$wrapper.height();
	var scrollBarHeight=scrollerHeight<=wrapperHeight?0:Math.floor(wrapperHeight*wrapperHeight/scrollerHeight);
	$scrollerBar.height(scrollBarHeight);
	
	$scroller[0].style.WebkitTransition="";
	$scrollerBar[0].style.WebkitTransition="";
	if(isTop){
		$scrollerBar[0].style.top="0px";
		$scroller[0].style.top="0px";
	}
	else
		$scrollerBar[0].style.top=-Math.floor($scroller.offsetTop*(wrapperHeight-scrollBarHeight)/(scrollerHeight-wrapperHeight))+"px";
}

function scrollYTS(e){
	if(e.target.tagName.toLocaleLowerCase()=="input" || e.target.tagName.toLocaleLowerCase()=="select" || e.target.tagName.toLocaleLowerCase()=="textarea")
		return;
	
	//加入 检索按钮的判断
	if(e.target.className.indexOf("search") != -1){
		return;
	}
	
	//加入 iframe的判断
	if(e.target.className.indexOf("iframe") != -1){
		return;
	}
	
	//加入不加入滚动的 class
	if(e.target.className.indexOf("un_scroll_flag") != -1){
		return;
	}

	var target=e.target;
	while(target!==e.currentTarget){
	    if($(target).attr("browserclick"))
	        return;
	    target=target.parentElement;
	}
	e.preventDefault();
	e.currentTarget.addEventListener(MOVE_EV,scrollYTM);
	e.currentTarget.addEventListener(END_EV,scrollYTE);
	
	var point=window.supportTouch?e.touches[0]:e;
	$(e.currentTarget).data("startY",point.clientY);
	$(e.currentTarget).data("startX",point.clientX);
	$(e.currentTarget).data("startOffsetTop",e.currentTarget.offsetTop);
	$(e.currentTarget).data("startTime",new Date());
	$(e.currentTarget).data("direction",null);
	
	//e.stopPropagation();
}

function scrollYTE(e){
	e.preventDefault();
	var point=window.supportTouch?e.changedTouches[0]:e;
	
	e.currentTarget.removeEventListener(MOVE_EV,scrollYTM);
	e.currentTarget.removeEventListener(END_EV,scrollYTE);
	
	var touchStartY=$(e.currentTarget).data("startY");
	var touchStartX=$(e.currentTarget).data("startX");
	var touchStartTime=$(e.currentTarget).data("startTime");
	
	var touchEndOffsetTop=e.currentTarget.offsetTop;
	var touchEndTime=new Date();
	
	var scrollBar=$(e.currentTarget).data("scrollBar");
	
	var dir=$(e.currentTarget).data("direction");
	//click or taphold
	if(dir==null){
		if(touchEndTime.getTime()-touchStartTime.getTime()>750){
		    var eventTarget=e.target;
		    var event=createEvent(e,"taphold");
	        while(eventTarget!==e.currentTarget){
	        	event.currentTarget=eventTarget;
	        	if(!event.propagation)
					break;
	            if($(eventTarget).data("taphold")){
	            	var args=$(eventTarget).data("taphold")[1];
	            	if(args){
	            		$(eventTarget).data("taphold")[0].apply(window,[event].concat(args));
	            	}else{
	            		$(eventTarget).data("taphold")[0].call(window,event);
	            	}	
	            }
	            eventTarget=eventTarget.parentElement;
	        }
			return;
		}
		var eventTarget=e.target;
		var event=createEvent(e,"click");
		while(eventTarget!==e.currentTarget){
			event.currentTarget=eventTarget;
			if(!event.propagation)
				break;
		    if($(eventTarget).data("click")){
		        var args=$(eventTarget).data("click")[1];
            	if(args){
            		$(eventTarget).data("click")[0].apply(window,[event].concat(args));
            	}else{
            		$(eventTarget).data("click")[0].call(window,event);
            	}	
		    }
		    eventTarget=eventTarget.parentElement;
		}
		return;
	}
	//x swipe
	if(dir=="x"){
		var eventTarget=e.target;
	    var event=createEvent(e,"swipeend");
        while(eventTarget!==e.currentTarget){
        	event.currentTarget=eventTarget;
        	if(!event.propagation)
				break;
            if($(eventTarget).data("swipeend")){
            	var args=$(eventTarget).data("swipeend")[1];
            	if(args){
            		$(eventTarget).data("swipeend")[0].apply(window,[event,point.clientX-touchStartX].concat(args));
            	}else{
            		$(eventTarget).data("swipeend")[0].call(window,event,point.clientX-touchStartX);
            	}	
            }
            eventTarget=eventTarget.parentElement;
        }
		return;
	}
	//y scroll
	if(e.currentTarget.offsetTop>0){
	    var scrollExTopHandler=$(e.currentTarget).data("scrollExTopHandler");
	    if(scrollExTopHandler){
	        scrollExTopHandler.call(this,function(){
	            $(e.currentTarget).animate({top:"0px"}, "fast");
	            scrollBar.animate({top:"0px"}, "fast");
            });
            return;
        }
		$(e.currentTarget).animate({top:"0px"}, "fast");
		scrollBar.animate({top:"0px"}, "fast");
		return;
	}

	if($(e.currentTarget).parent()[0].clientHeight-e.currentTarget.clientHeight>0){
		$(e.currentTarget).animate({top:"0px"}, "fast");
		return;
	}
	
	if(e.currentTarget.offsetTop<$(e.currentTarget).parent()[0].clientHeight-e.currentTarget.clientHeight){
		var scrollExBottomHandler=$(e.currentTarget).data("scrollExBottomHandler");
		if(scrollExBottomHandler){
			scrollExBottomHandler.call(this,function(){
				$(e.currentTarget).animate({top:$(e.currentTarget).parent()[0].clientHeight-e.currentTarget.clientHeight+"px"}, "fast");
				scrollBar.animate({top:$(e.currentTarget).parent()[0].clientHeight-scrollBar[0].clientHeight+"px"}, "fast");
			});
			return;
	    }
		$(e.currentTarget).animate({top:$(e.currentTarget).parent()[0].clientHeight-e.currentTarget.clientHeight+"px"}, "fast");
		scrollBar.animate({top:$(e.currentTarget).parent()[0].clientHeight-scrollBar[0].clientHeight+"px"}, "fast");
		return;
	}
	
	var touchInterval=touchEndTime.getTime()-touchStartTime.getTime();
	var touchDistance=point.clientY-touchStartY;
	if(touchInterval<=500){
		var scrollDistance=Math.abs(Math.floor(350*touchDistance*touchDistance/touchInterval/touchInterval));
		//var scrollInterval=Math.abs(Math.floor(scrollDistance*touchInterval/touchDistance));
		var endTop;
		if(touchDistance>0){
			if(touchEndOffsetTop+scrollDistance>0)
				endTop=0;
			else
				endTop=touchEndOffsetTop+scrollDistance;
		}else{
			if(touchEndOffsetTop-scrollDistance<$(e.currentTarget).parent()[0].clientHeight-e.currentTarget.clientHeight)
				endTop=$(e.currentTarget).parent()[0].clientHeight-e.currentTarget.clientHeight;
			else
				endTop=touchEndOffsetTop-scrollDistance;
		}
		
		var scrollInterval=Math.abs(Math.floor((endTop-touchEndOffsetTop)*touchInterval/touchDistance));
		
		$(e.currentTarget)[0].style.WebkitTransition="top "+scrollInterval/1000+"s ease-out";
		$(e.currentTarget)[0].style.top=endTop+"px";
		scrollBar[0].style.WebkitTransition="top "+scrollInterval/1000+"s ease-out";
		scrollBar[0].style.top=-Math.floor(endTop*($(e.currentTarget).parent().height()-scrollBar.height())/($(e.currentTarget).height()-$(e.currentTarget).parent().height()))+"px"
		
		//$(e.currentTarget).animate({top:endTop+"px"},scrollInterval,"easeOutCubic");
		//scrollBar.animate({top:-Math.floor(endTop*($(e.currentTarget).parent().height()-scrollBar.height())/($(e.currentTarget).height()-$(e.currentTarget).parent().height()))+"px"},scrollInterval,"easeOutCubic");
	}
	
}

function scrollYTM(e){
	e.preventDefault();
	var point=window.supportTouch?e.touches[0]:e;
	
	var touchStartY=$(e.currentTarget).data("startY");
	var touchStartX=$(e.currentTarget).data("startX");
	var touchStartOffsetTop=$(e.currentTarget).data("startOffsetTop");
	var touchStartTime=$(e.currentTarget).data("startTime");
	var scrollBar=$(e.currentTarget).data("scrollBar");
	var bounce=$(e.currentTarget).data("bounce");
	
	var dir;
	if(!$(e.currentTarget).data("direction")){
		if(Math.abs(point.clientX-touchStartX)<5 && Math.abs(point.clientY-touchStartY)<5)
			return;
		dir=Math.abs(point.clientX-touchStartX)-Math.abs(point.clientY-touchStartY);
		if(dir>0)
			$(e.currentTarget).data("direction","x");
		else
			$(e.currentTarget).data("direction","y");
	}
	dir=$(e.currentTarget).data("direction");
	if(dir=="y"){
		$(e.currentTarget)[0].style.WebkitTransition="";
		scrollBar[0].style.WebkitTransition="";
		
		//禁止越过边界
		if(bounce){
			if($(e.currentTarget).parent()[0].clientHeight-e.currentTarget.clientHeight>0){
				return;
			}
			if(touchStartOffsetTop+point.clientY-touchStartY>0){
			    if(touchStartOffsetTop==0){
    			    var scrollingExTopHandler=$(e.currentTarget).data("scrollingExTopHandler");
    			    if(scrollingExTopHandler){
    			        scrollingExTopHandler.call(window,point.clientY-touchStartY);
    			    }
    			}
				e.currentTarget.style.top="0px";
				scrollBar[0].style.top="0px";
				return;
			}
			if(touchStartOffsetTop+point.clientY-touchStartY<$(e.currentTarget).parent()[0].clientHeight-e.currentTarget.clientHeight){
			    if(touchStartOffsetTop==$(e.currentTarget).parent()[0].clientHeight-e.currentTarget.clientHeight){
    			    var scrollingExBottomHandler=$(e.currentTarget).data("scrollingExBottomHandler");
                    if(scrollingExBottomHandler){
                        scrollingExBottomHandler.call(window,point.clientY-touchStartY);
                    }
			    }
				$(e.currentTarget).css({top:$(e.currentTarget).parent()[0].clientHeight-e.currentTarget.clientHeight+"px"});
				scrollBar.css({top:$(e.currentTarget).parent()[0].clientHeight-scrollBar[0].clientHeight+"px"});
				return;
			}
		}
		
		e.currentTarget.style.top=touchStartOffsetTop+point.clientY-touchStartY+"px";
		scrollBar[0].style.top=-Math.floor((touchStartOffsetTop+point.clientY-touchStartY)*($(e.currentTarget).parent().height()-scrollBar.height())/($(e.currentTarget).height()-$(e.currentTarget).parent().height()))+"px";
	}else{
		var eventTarget=e.target;
	    var event=createEvent(e,"swiping");
        while(eventTarget!==e.currentTarget){
        	event.currentTarget=eventTarget;
        	if(!event.propagation)
				break;
            if($(eventTarget).data("swiping")){
            	var args=$(eventTarget).data("swiping")[1];
            	if(args){
            		$(eventTarget).data("swiping")[0].apply(window,[event,point.clientX-touchStartX].concat(args));
            	}else{
            		$(eventTarget).data("swiping")[0].call(window,event,point.clientX-touchStartX);
            	}	
            }
            eventTarget=eventTarget.parentElement;
        }
	}
}

function scrollXTS(e){
    if(e.target.tagName.toLocaleLowerCase()=="input" || e.target.tagName.toLocaleLowerCase()=="select" || e.target.tagName.toLocaleLowerCase()=="textarea")
        return;
        
	//加入 检索按钮的判断
	if(e.target.className.indexOf("search") != -1){
		return;
	}
	
	//加入 iframe的判断
	if(e.target.className.indexOf("iframe") != -1){
		return;
	}
	
	//加入不加入滚动的 class
	if(e.target.className.indexOf("un_scroll_flag") != -1){
		return;
	}
	
	e.preventDefault();
	e.currentTarget.addEventListener(MOVE_EV,scrollXTM);
    e.currentTarget.addEventListener(END_EV,scrollXTE);
	
	var point=window.supportTouch?e.touches[0]:e;
	$(e.currentTarget).data("startY",point.clientY);
	$(e.currentTarget).data("startX",point.clientX);
	$(e.currentTarget).data("startOffsetLeft",e.currentTarget.offsetLeft);
	$(e.currentTarget).data("startTime",new Date());
	$(e.currentTarget).data("direction",null);
	
	e.stopPropagation();
}

function scrollXTE(e){
    var point=window.supportTouch?e.changedTouches[0]:e;
    e.preventDefault();
    e.currentTarget.removeEventListener(MOVE_EV,scrollXTM);
    e.currentTarget.removeEventListener(END_EV,scrollXTE);
    
    var touchStartX=$(e.currentTarget).data("startX");
	var touchStartTime=$(e.currentTarget).data("startTime");
	
	var touchEndOffsetLeft=e.currentTarget.offsetLeft;
	var touchEndTime=new Date();
    
    if(point.clientX-touchStartX<10 && point.clientX-touchStartX>-10){
        if(touchEndTime.getTime()-touchStartTime.getTime()>750){
            var eventTarget=e.target;
            var event=createEvent(e,"taphold");
            while(eventTarget!==e.currentTarget){
            	event.currentTarget=eventTarget;
            	if(!event.propagation)
					break;
	            if($(eventTarget).data("taphold")){
	            	var args=$(eventTarget).data("taphold")[1];
	            	if(args){
	            		args.push(event);
	            		$(eventTarget).data("taphold")[0].apply(window,args);
	            	}else{
	            		$(eventTarget).data("taphold")[0].call(window,event);
	            	}	
	            }
	            eventTarget=eventTarget.parentElement;
            }
            return;
        }
        var eventTarget=e.target;
        var event=createEvent(e,"click");
		while(eventTarget!==e.currentTarget){
			event.currentTarget=eventTarget;
			if(!event.propagation)
				break;
		    if($(eventTarget).data("click")){
		        var args=$(eventTarget).data("click")[1];
            	if(args){
            		args.push(event);
            		$(eventTarget).data("click")[0].apply(window,args);
            	}else{
            		$(eventTarget).data("click")[0].call(window,event);
            	}	
		    }
		    eventTarget=eventTarget.parentElement;
		}
        return;
    }
    
    if(e.currentTarget.offsetLeft>0){
        $(e.currentTarget).animate({left:"0px"}, "fast");
        return;
    }

    if($(e.currentTarget).parent()[0].clientWidth-e.currentTarget.clientWidth>0){
        $(e.currentTarget).animate({left:"0px"}, "fast");
        return;
    }
    
    if(e.currentTarget.offsetLeft<$(e.currentTarget).parent()[0].clientWidth-e.currentTarget.clientWidth){
        $(e.currentTarget).animate({left:$(e.currentTarget).parent()[0].clientWidth-e.currentTarget.clientWidth+"px"}, "fast");
        return;
    }
    
}

function scrollXTM(e){
	e.preventDefault();
	var touchStartX=$(e.currentTarget).data("startX");
	var touchStartOffsetLeft=$(e.currentTarget).data("startOffsetLeft");
	var point=window.supportTouch?e.touches[0]:e;
    if(point.clientX-touchStartX>=10 || point.clientX-touchStartX<=-10){
    	//禁止越界
    	if($(e.currentTarget).parent()[0].clientWidth-e.currentTarget.clientWidth>0){
			return;
		}
		if(touchStartOffsetLeft+point.clientX-touchStartX>0){
			e.currentTarget.style.left="0px";
			return;
		}
		if(touchStartOffsetLeft+point.clientX-touchStartX<$(e.currentTarget).parent()[0].clientWidth-e.currentTarget.clientWidth){
			$(e.currentTarget).css({left:$(e.currentTarget).parent()[0].clientWidth-e.currentTarget.clientWidth+"px"});
			return;
		}
        e.currentTarget.style.left=touchStartOffsetLeft+point.clientX-touchStartX+"px";
    }
}

$.fn.addEventListener=function(eventType,handler,args,tscallback,tecallback){
	if(eventType=="quickClick"){
		this.each(function(){
	    	this.addEventListener(START_EV,domQuickClickTS);
	    });
	}
		
    this.each(function(){
    	$(this).data(eventType,[handler,args,tscallback,tecallback]);
    });
}

$.fn.removeEventListener=function(eventType){
    this.removeData(eventType);
}

function createEvent(e,type){
    var point=window.supportTouch?e.changedTouches[0]:e;
    var event={};
    event.type=type;
    event.target=e.target;
    event.clientX=point.clientX;
    event.clientY=point.clientY;
    event.propagation=true;	
    event.stopPropagation=function(){
    	event.propagation=false;	
    }
    
    return event;
}

function createSelectMenu(id,pageId,options){
	var selectMenu=$("<select id='"+id+"' data-native-menu='false' onchange='"+id+"Change();'></select>");
	selectMenu.appendTo($("#"+pageId+" div[data-role=content]"));
	var option=new Option();
	selectMenu[0].options[0]=option;
	for(var i=0;i<options.length;i++){
		option=new Option();
		option.value=options[i];
		option.text=options[i];
		selectMenu[0].options[i+1]=option;
	}
	$("#"+id).selectmenu();
	$("#"+pageId+" .ui-select").hide();
}

function getScore(score){
	if(score==null || score=="")
		return "暂无评分";
	var scoreInt=parseInt(score);
	var scoreStr="";
	for(var i=0;i<scoreInt;i++){
		scoreStr+="★";
	}
	return scoreStr;
	
}

function domQuickClickTS(e){
	var tscallback=$(e.currentTarget).data("quickClick")[2];
	if(tscallback)
		tscallback.call(window,e.currentTarget);
	
	e.currentTarget.addEventListener(END_EV,domQuickClickTE);
	
	var point=window.supportTouch?e.touches[0]:e;
	$(e.currentTarget).data("startY",point.clientY);
	$(e.currentTarget).data("startX",point.clientX);
	
	//e.preventDefault();
	return;
}

function domQuickClickTE(e){
	var tecallback=$(e.currentTarget).data("quickClick")[3];
	if(tecallback)
		tecallback.call(window,e.currentTarget);
	
	e.currentTarget.removeEventListener(END_EV,domQuickClickTE);

	var touchStartY=$(e.currentTarget).data("startY");
	var touchStartX=$(e.currentTarget).data("startX");
	var point=window.supportTouch?e.changedTouches[0]:e;
	
	if(Math.abs(point.clientX-touchStartX)<10 && Math.abs(point.clientY-touchStartY)<10){
		var eventTarget=e.currentTarget;
	    if($(eventTarget).data("quickClick")){
			var event=createEvent(e,"quickClick");
			event.currentTarget=eventTarget;
	        var args=$(eventTarget).data("quickClick")[1];
        	if(args){
        		$(eventTarget).data("quickClick")[0].apply(window,[event].concat(args));
        	}else{
        		$(eventTarget).data("quickClick")[0].call(window,event);
        	}
			if(event.stopPropagation)
				e.stopPropagation();
	    }
	}
	
	//e.preventDefault();
	return;
}

function initSwipe(id,callback){
    $("#"+id)[0].addEventListener(START_EV,swipeTS);
    $("#"+id).data("callback",callback);
}
function swipeTS(e){
    e.currentTarget.addEventListener(END_EV,swipeTE);
    
    var point=window.supportTouch?e.touches[0]:e;
    $(e.currentTarget).data("startY",point.clientY);
    $(e.currentTarget).data("startX",point.clientX);
    $(e.currentTarget).data("startTime",new Date());
}
function swipeTE(e){
    e.currentTarget.removeEventListener(END_EV,swipeTE);
    
    var point=window.supportTouch?e.changedTouches[0]:e;
    
    var touchStartX=$(e.currentTarget).data("startX");
    var touchStartY=$(e.currentTarget).data("startY");
    var touchStartTime=$(e.currentTarget).data("startTime");
    
    var touchEndTime=new Date();
    
    $(e.currentTarget).data("callback").call(window,touchStartX,touchStartY,point.clientX,point.clientY,touchStartTime,touchEndTime);
}

/**
 * 判断页面元素是否可见
 * @param obj dom对象或者jQuery对象
 */
function isVisible(obj){
    if(obj.tagName)
        return obj.style.display!="none";
    else
        return obj[0].style.display!="none";
    
}